export default {
  methods: {
    // 向下
    bottomCell(row, index, column) {
      try {
        if (this.tableData.length - 1 === index) return
        this.$set(row, '$cellEdit', false)
        this.$set(this.tableData[index + 1], '$cellEdit', true)
        this.moveCell(index + 1, column)
      } catch (error) {
        console.error('向下移动出错、未获取到该元素下面的元素，自定义新增方法未成功新增数据');
      }
    },
    topCell(row, index, column) {
      if (index === 0) return
      this.$set(row, '$cellEdit', false)
      this.$set(this.tableData[index - 1], '$cellEdit', true)
      this.moveCell(index - 1, column)
    },
    leftCell(row, index, column, delIndex) {
      const editCells = this.columns.filter(res => res.cell)
      const i = editCells.map(res => res.prop).indexOf(column.prop)
      if (i === 0) {
        this.topCell(row, index, editCells[editCells.length - 1])
        return
      }
      this.moveCell(index, editCells[i - (delIndex || 1)])
    },
    rightCell(row, index, column, addIndex) {
      const editCells = this.columns.filter(res => res.cell)
      const i = editCells.map(res => res.prop).indexOf(column.prop)
      if (i === editCells.length - 1) {
        this.bottomCell(row, index, editCells[0])
        return
      }
      this.moveCell(index, editCells[i + (addIndex || 1)])
    },
    moveCell(index, cell) {
      const type = cell.type || 'input'
      this.$nextTick(() => {
        try {
          if (cell.slotCell) {
            this.$refs[`form_item_${cell.prop}_${index}`].some(res => {
              const obj = res.$slots.default[0].child
              obj.focus()
              if (type === 'input' || type === 'number') obj.select()
            })
          } else {
            this.$refs[`cell_${cell.prop}`].some(res => {
              const obj = res.$refs.temp.$children[0]
              obj.focus()
              if (type === 'input' || type === 'number') obj.select()
            })
          }
        } catch (error) {
          console.error(error);
        }
      })
    },
  }
}
